package com.hoshiibuy.neo4j.controller;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.neo4j.node.RNode;
import com.hoshiibuy.neo4j.node.Tenant;
import com.hoshiibuy.neo4j.node.relation.REdge;
import com.hoshiibuy.neo4j.util.Neo4jUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.neo4j.driver.v1.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/neo4j")
@Api(value = "Neo4j接口管理", tags = "neo4j接口")
public class Neo4jController {


    @Autowired
    private Session session;


    final static ObjectMapper mapper = new ObjectMapper();

    static {
        mapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false);
    }


    @RequestMapping(value = "/create/tenant", method = RequestMethod.POST)
    @ApiOperation("新增租户")
    public Response createTenant(@RequestBody Tenant tenant) {
        try {

            Neo4jUtil.createTenant(mapper, session, tenant);

        } catch (Exception e) {
            return Response.failed(e.getMessage());

        }
        return Response.success("创建成功");

    }

    @RequestMapping(value = "/create/platform", method = RequestMethod.POST)
    @ApiOperation("新增平台")
    @ApiImplicitParam(name = "RNode", value = "平台信息", dataType = "RNode")
    public Response createPlatform(@RequestBody RNode rNode) {
        try {

            Neo4jUtil.createPlatform(mapper, session, rNode);

        } catch (Exception e) {
            return Response.failed(e.getMessage());

        }
        return Response.success("创建成功");


    }

    @RequestMapping(value = "/create/organization", method = RequestMethod.POST)
    @ApiOperation("新增组织架构")
    @ApiImplicitParam(name = "RNode", value = "组织架构信息", dataType = "RNode")
    public Response createOrganization(@RequestBody RNode rNode) {
        try {

            Neo4jUtil.createOrganization(mapper, session, rNode);

        } catch (Exception e) {
            return Response.failed(e.getMessage());

        }

        return Response.success("创建成功");

    }

    @RequestMapping(value = "/create/role", method = RequestMethod.POST)
    @ApiOperation("新增角色")
    @ApiImplicitParam(name = "RNode", value = "角色信息", dataType = "RNode")
    public Response createRole(@RequestBody RNode rNode) {
        try {

            Neo4jUtil.createRole(mapper, session, rNode);

        } catch (Exception e) {
            return Response.failed(e.getMessage());

        }


        return Response.success("创建成功");

    }

    @RequestMapping(value = "/create/permission", method = RequestMethod.POST)
    @ApiOperation("新增权限")
    @ApiImplicitParam(name = "RNode", value = "权限信息", dataType = "RNode")
    public Response createPermission(@RequestBody RNode rNode) {
        try {

            Neo4jUtil.createPermission(mapper, session, rNode);

        } catch (Exception e) {
            return Response.failed(e.getMessage());

        }

        return Response.success("创建成功");

    }

    @RequestMapping(value = "/create/userarea", method = RequestMethod.POST)
    @ApiOperation("创建用户区")
    @ApiImplicitParam(name = "RNode", value = "用户区信息", dataType = "RNode")
    public Response createUserArea(@RequestBody RNode rNode) {
        try {

            Neo4jUtil.createUserArea(mapper, session, rNode);

        } catch (Exception e) {
            return Response.failed(e.getMessage());

        }

        return Response.success("创建成功");

    }

    @RequestMapping(value = "/create/user", method = RequestMethod.POST)
    @ApiOperation("创建用户")
    @ApiImplicitParam(name = "RNode", value = "用户信息", dataType = "RNode")
    public Response createUser(@RequestBody RNode rNode) {
        try {

            Neo4jUtil.createUser(mapper, session, rNode);

        } catch (Exception e) {
            return Response.failed(e.getMessage());

        }

        return Response.success("创建成功");

    }

    @RequestMapping(value = "/create/staff", method = RequestMethod.POST)
    @ApiOperation("创建员工")
    @ApiImplicitParam(name = "RNode", value = "员工信息", dataType = "RNode")
    public Response createStaff(@RequestBody RNode rNode) {
        try {

            Neo4jUtil.createStaff(mapper, session, rNode);

        } catch (Exception e) {
            return Response.failed(e.getMessage());

        }

        return Response.success("创建成功");

    }

    @RequestMapping(value = "/create/staffRole", method = RequestMethod.POST)
    @ApiOperation("创建员工角色")
    @ApiImplicitParam(name = "RNode", value = "员工角色信息", dataType = "RNode")
    public Response createstaffRole(@RequestBody RNode rNode) {
        try {

            Neo4jUtil.createstaffRole(mapper, session, "", "");

        } catch (Exception e) {
            return Response.failed(e.getMessage());

        }

        return Response.success("创建成功");

    }

    @RequestMapping(value = "/create/cooperationOrg", method = RequestMethod.POST)
    @ApiOperation("创建合作机构")
    @ApiImplicitParam(name = "RNode", value = "合作机构信息", dataType = "RNode")
    public Response createCooperationOrg(@RequestBody RNode rNode) {
        try {

            Neo4jUtil.createStaff(mapper, session, rNode);

        } catch (Exception e) {
            return Response.failed(e.getMessage());

        }

        return Response.success("创建成功");

    }

    /**
     * 创建节点
     */
    @RequestMapping(value = "/create/node", method = RequestMethod.POST)
    @ApiOperation("新增节点")
    @ApiImplicitParam(name = "RNode", value = "节点信息", dataType = "RNode")
    public Response createNode(@RequestBody RNode rNode) {

        try {

            Neo4jUtil.createNode(mapper, session, rNode);

        } catch (Exception e) {
            return Response.failed(e.getMessage());

        }
        return Response.success("创建成功");

    }

    /**
     * 创建关系
     */
    @RequestMapping(value = "/create/relation", method = RequestMethod.POST)
    @ApiOperation("创建关系")
    @ApiImplicitParam(name = "REdge", value = "创建关系", dataType = "REdge")
    public Response createRelation(@RequestBody REdge redge, @RequestParam("parId") String parId,
                                   @RequestParam("childId") String childId) {

        try {

            RNode srcNode = new RNode();
            srcNode.setUuid(Long.parseLong(parId));
            srcNode.setEdge(redge);

            RNode tarNode = new RNode();
            tarNode.setUuid(Long.parseLong(childId));

            Neo4jUtil.createRelation(mapper, session, srcNode, tarNode);

        } catch (Exception e) {
            return Response.failed(e.getMessage());

        }
        return Response.success("成功");

    }

    /**
     * 查询节点
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/query/node", method = RequestMethod.POST)
    @ApiOperation("查询节点")
    @ApiImplicitParam(name = "uuid", value = "查询节点", dataType = "String")
    public Response queryNode(@RequestParam(value = "uuid", defaultValue = "0") String uuid) {

        RNode rNode = null;
        try {

            rNode = new RNode();
            rNode.setUuid(Long.parseLong(uuid));
            rNode.setLabel("大中台");
            rNode = Neo4jUtil.queryNode(mapper, session, rNode);
            System.out.println(rNode);

        } catch (Exception e) {
            return Response.failed(e.getMessage());
        }

        return Response.data(rNode);
    }

    /**
     * 查询关系
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/query/relation", method = RequestMethod.POST)
    @ApiOperation("查询关系")
    @ApiImplicitParam(name = "RNode", value = "查询关系", dataType = "RNode")
    public Response queryRelation(@RequestParam("parId") String parId, @RequestParam("childId") String childId,
                                  @RequestParam("edgeName") String edgeName) {
        REdge edge = null;
        try {

            RNode srcNode = new RNode();
            srcNode.setUuid(Long.parseLong(parId));

            edge = new REdge();
            edge.setName(edgeName);
            srcNode.setEdge(edge);

            RNode tarNode = new RNode();
            tarNode.setUuid(Long.parseLong(childId));

            Neo4jUtil.queryRelation(mapper, session, srcNode, tarNode);

        } catch (Exception e) {
            return Response.failed(e.getMessage());
        }

        return Response.data(edge);
    }
}
